package com.xdap.common.dao;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.api.moudle.user.pojo.ApaasUsersExpandStorage;
import com.xdap.api.moudle.user.pojo.XdapUsers;
import com.xdap.common.pojo.po.ComUserPo;
import com.xdap.common.util.ComStrUtil;
import com.xdap.motor.utils.StringUtils;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @USER liShuShu
 * @TIME 2023/2/14 0014 10:10
 * @DESC 对系统内人员进行处理
 **/
@Repository
@RequiredArgsConstructor
public class ComUserDao {
    private final RuntimeDatasourceService runtimeDatasourceService;
    @Value("xdap_app_${apaas.single.tenantId}")
    private String tenantSchema;
    public static final String XDAP_USERS = "xdap_users";
    public static final String APAAS_USERS_EXPAND_STORAGE = "apaas_users_expand_storage";

    /**********获得完整人员信息****************/

    public XdapUsers queryUserByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            account = "-1";
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .eq("account", account)
                .table(XDAP_USERS)
                .doQueryFirst(XdapUsers.class);
    }

    public String getUserIdByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            account = null;
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.select("id");
        return mpaasQuery
                .eq("account", account)
                .table(XDAP_USERS)
                .doQueryFirst(String.class);
    }

    public List<String> getAccountByUserId(List<String> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return new ArrayList<>();
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.select("account");
        return mpaasQuery
                .in("id", userIds)
                .table(XDAP_USERS)
                .doQuery(String.class);
    }

    public String getUserIdByAcOrNumber(String account) {
        if (StringUtils.isBlank(account)) {
            account = null;
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.select("id");
        return mpaasQuery
                .eq("user_number", account)
                .or()
                .eq("account", account)
                .table(XDAP_USERS)
                .doQueryFirst(String.class);
    }

    public ComUserPo getUserById(String id) {
        if (StringUtils.isBlank(id)) {
            id = "-1";
        }
        String data = ComStrUtil.getData(id);

        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .eq("id", data)
                .table(XDAP_USERS)
                .doQueryFirst(ComUserPo.class);
    }

    public List<ComUserPo> queryUserByIds(List<String> ids) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .in("id", ids)
                .table(XDAP_USERS)
                .doQuery(ComUserPo.class);
    }

    public XdapUsers queryUserByName(String name) {
        if (StringUtils.isBlank(name)) {
            name = "-1";
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .eq("username", name)
                .table(XDAP_USERS)
                .doQueryFirst(XdapUsers.class);
    }


    public List<XdapUsers> queryUsersByName(String name) {
        if (StringUtils.isBlank(name)) {
            name = "-1";
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .eq("username", name)
                .table(XDAP_USERS)
                .doQuery(XdapUsers.class);
    }

    public XdapUsers queryUserByNumber(String account) {
        if (StringUtils.isBlank(account)) {
            account = "-1";
        }
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        return mpaasQuery
                .eq("user_number", account)
                .table(XDAP_USERS)
                .doQueryFirst(XdapUsers.class);
    }

    /**
     * @Description 根据职位获取人员
     * @Date 2023/10/18 10:27
     * @Author shangsheng.li
     **/
    public List<ApaasUsersExpandStorage> queryUserTitleList(String content) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildBusinessMpaasQuery(tenantSchema, APAAS_USERS_EXPAND_STORAGE);
        return mpaasQuery
                .like("content", content)
                .table(APAAS_USERS_EXPAND_STORAGE)
                .doQuery(ApaasUsersExpandStorage.class);
    }


    public List<String> queryUserIdList() {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.select("id");
        return mpaasQuery
                .eq("exit_status", "ENABLE")
                .table(XDAP_USERS)
                .doQuery(String.class);
    }


    public Integer updateUser(XdapUsers userById) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildBusinessMpaasQuery(tenantSchema, XDAP_USERS);
        return mpaasQuery
                .table(XDAP_USERS)
                .eq("id", userById.getId())
                .doUpdate(userById);
    }
}
